/*
 * @(#)PaintPanel.java	1.0 Apr 27, 2008
 *
 *	The MIT License
 *
 *	Copyright (c) 2008 Malachi de AElfweald <malachid@gmail.com>
 *
 *	Permission is hereby granted, free of charge, to any person obtaining a copy
 *	of this software and associated documentation files (the "Software"), to deal
 *	in the Software without restriction, including without limitation the rights
 *	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *	copies of the Software, and to permit persons to whom the Software is
 *	furnished to do so, subject to the following conditions:
 *
 *	The above copyright notice and this permission notice shall be included in
 *	all copies or substantial portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *	THE SOFTWARE.
 */
package org.eoti.swing.panels;

import javax.swing.*;
import java.awt.*;

public class PaintPanel
extends JPanel
{
	public enum Location{North,NorthEast,East,SouthEast,South,SouthWest,West,NorthWest,Center}

	protected Paint backgroundPaint;
	protected Location gradientLoc1, gradientLoc2;
	protected Color gradientColor1, gradientColor2;

	public PaintPanel(LayoutManager layout, boolean isDoubleBuffered)
	{
		super(layout, isDoubleBuffered);
	}

	public PaintPanel(LayoutManager layout)
	{
		super(layout);
	}

	public PaintPanel(boolean isDoubleBuffered)
	{
		super(isDoubleBuffered);
	}

	public PaintPanel()
	{
		super();
	}

	public void setGradientBackground(Location loc1, Color color1, Location loc2, Color color2)
	{
		unsetAll();
		gradientLoc1 = loc1;
		gradientColor1 = color1;
		gradientLoc2 = loc2;
		gradientColor2 = color2;
	}

	public void setBackground(Paint background)
	{
		unsetAll();
		this.backgroundPaint = background;
	}

	protected void unsetAll()
	{
		backgroundPaint = null;
		gradientLoc1 = null;
		gradientLoc2 = null;
		gradientColor1 = null;
		gradientColor2 = null;
	}

	protected void paintComponent(Graphics g)
	{
		Graphics2D g2 = (Graphics2D)g;
		Rectangle bounds = getBounds();

		if(backgroundPaint != null)
		{
			g2.setPaint(backgroundPaint);
			g.fillRect(bounds.x, bounds.y, bounds.x+bounds.width, bounds.y+bounds.height);
//			super.paintComponent(g);
			return;
		}

		if( (gradientLoc1 != null) && (gradientLoc2 != null) && (gradientColor1 != null) && (gradientColor2 != null))
		{
			float x1 = 0;
			float x2 = 0;
			float y1 = 0;
			float y2 = 0;
			switch(gradientLoc1)
			{
				case NorthWest:
				case West:
				case SouthWest:
					x1 = bounds.x;
					break;
				case North:
				case Center:
				case South:
					x1 = bounds.x + (bounds.width / 2);
					break;
				case NorthEast:
				case East:
				case SouthEast:
					x1 = bounds.x + bounds.width;
					break;
			}
			switch(gradientLoc1)
			{
				case NorthWest:
				case North:
				case NorthEast:
					y1 = bounds.y;
					break;
				case West:
				case Center:
				case East:
					y1 = bounds.y + (bounds.height / 2);
					break;
				case SouthWest:
				case South:
				case SouthEast:
					y1 = bounds.y + bounds.height;
					break;
			}
			switch(gradientLoc2)
			{
				case NorthWest:
				case West:
				case SouthWest:
					x2 = bounds.x;
					break;
				case North:
				case Center:
				case South:
					x2 = bounds.x + (bounds.width / 2);
					break;
				case NorthEast:
				case East:
				case SouthEast:
					x2 = bounds.x + bounds.width;
					break;
			}
			switch(gradientLoc2)
			{
				case NorthWest:
				case North:
				case NorthEast:
					y2 = bounds.y;
					break;
				case West:
				case Center:
				case East:
					y2 = bounds.y + (bounds.height / 2);
					break;
				case SouthWest:
				case South:
				case SouthEast:
					y2 = bounds.y + bounds.height;
					break;
			}

			g2.setPaint(
				new GradientPaint(
					x1,
					y1,
					gradientColor1,
					x2,
					y2,
					gradientColor2
				)
			);
			g.fillRect(bounds.x, bounds.y, bounds.x+bounds.width, bounds.y+bounds.height);
			return;
		}

		super.paintComponent(g);
	}
}
